﻿' TouchFlo Detacher
' Copyright (C) 2009, Avian Waves Software
' SEE LICENSE.TXT for software licensing information regarding original TouchFlo Detacher source code and binaries
' SEE OpenNetCF_LICENSE.TXT for licensing information regarding OpenNetCF binaries

Imports TouchFloDetacher.Settings

Public Class TFDConfig
  Private _warnOnRadioChange As Boolean = True
  Private _lastIndex As Integer = 0
  Private _touchFloMode As Integer = 0

  Private _todayLSKApp As String = ""
  Private _todayLSKText As String = ""
  Private _todayRSKApp As String = ""
  Private _todayRSKText As String = ""
  Private _TF3DLSKApp As String = ""
  Private _TF3DLSKText As String = ""
  Private _TF3DRSKApp As String = ""
  Private _TF3DRSKText As String = ""

  ' Event fired when the form loads
  Private Sub TFDConfig_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    LabelTitle.Text = "TouchFlo Detacher v" & System.Reflection.Assembly.GetExecutingAssembly.GetName.Version.ToString & vbCrLf & "Copyright (C) 2009, Avian Waves"
    Me.TimerLoad.Enabled = True
  End Sub

  Private Sub TimerLoad_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TimerLoad.Tick
    Me.TimerLoad.Enabled = False

    Me.LoadRegistry()
    If TFDRegistrySettings.TF3DTodayEnabled Then
      If MessageBox.Show("The TouchFlo 3D Today item is enabled.  In order to use TouchFlo Detacher, it must be disabled.  Would you like TFDConfig to disable it for you?", "TouchFlo Today Item", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) = Windows.Forms.DialogResult.Yes Then
        TFDRegistrySettings.TF3DTodayEnabled = False
        If TFDRegistrySettings.TF3DTodayEnabled Then
          MessageBox.Show("Error: Unable to disable Today Item!  Exiting...", "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1)
          Me.Close()
        End If
      Else
        MessageBox.Show("Exiting TouchFlo Detacher Config...", "Exiting", MessageBoxButtons.OK, MessageBoxIcon.None, MessageBoxDefaultButton.Button1)
        Me.Close()
      End If
    End If

    If TFDRegistrySettings.TF2DTodayEnabled Then
      If MessageBox.Show("The TouchFlo 2D Today item is enabled.  In order to use TouchFlo Detacher, it must be disabled.  Would you like TFDConfig to disable it for you?", "TouchFlo Today Item", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) = Windows.Forms.DialogResult.Yes Then
        TFDRegistrySettings.TF2DTodayEnabled = False
        If TFDRegistrySettings.TF2DTodayEnabled Then
          MessageBox.Show("Error: Unable to disable Today Item!  Exiting...", "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1)
          Me.Close()
        End If
      Else
        MessageBox.Show("Exiting TouchFlo Detacher Config...", "Exiting", MessageBoxButtons.OK, MessageBoxIcon.None, MessageBoxDefaultButton.Button1)
        Me.Close()
      End If
    End If

    PanelOuter.Enabled = True
  End Sub

  ' Event fired when the user saves the form
  Private Sub MenuItemSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItemSave.Click
    '    Dim found As Boolean = False

    Me.SaveRegistry()

    'For Each p As OpenNETCF.ToolHelp.ProcessEntry In OpenNETCF.ToolHelp.ProcessEntry.GetProcesses
    '  If p.ExeFile.ToLower = "tfdetachsvc.exe" Then
    '    found = True
    '    Exit For
    '  End If
    'Next

    'If Not found Then
    '  If MessageBox.Show("The TouchFlo Detacher Service is currently not running.  Would you like to start it now?", "Start service?", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) = Windows.Forms.DialogResult.Yes Then
    '    Try
    '      Dim p As New Process
    '      p.StartInfo.FileName = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase) & "\TFDetachSvc.exe"
    '      p.Start()
    '    Catch ex As Exception
    '      Windows.Forms.Clipboard.SetDataObject(ex.ToString)
    '      MessageBox.Show("Unable to start service.  Exception details have been copied to the clipboard!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1)
    '    End Try
    '  End If
    'End If

    Me.Close()
  End Sub

  ' Event fired when user cancels the form
  Private Sub MenuItemCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItemCancel.Click
    Me.Close()
  End Sub

  ' Event fired when user clicks on the linklabel to visit TFD's homepage
  Private Sub LinkLabelHomePage_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LinkLabelHomePage.Click
    Try
      Dim p As New Process
      p.StartInfo.FileName = "http://www.codeplex.com/TFDetacher"
      p.Start()
    Catch
      MessageBox.Show("Unable to open URL", "Error")
    End Try
  End Sub



  Private Sub ButtonTFLeftSoftKey_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonTFLeftSoftKey.Click
    Dim ka As New KeyAssigner
    ka.LabelAssign.Text = "Assign TouchFlo left soft Key"
    ka.TextBoxApp.Text = Me._TF3DLSKApp
    ka.TextBoxText.Text = Me._TF3DLSKText
    If ka.ShowDialog = Windows.Forms.DialogResult.OK Then
      Me._TF3DLSKApp = ka.TextBoxApp.Text
      Me._TF3DLSKText = ka.TextBoxText.Text
    End If
  End Sub

  Private Sub ButtonTFRightSoftKey_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonTFRightSoftKey.Click
    Dim ka As New KeyAssigner
    ka.LabelAssign.Text = "Assign TouchFlo right soft Key"
    ka.TextBoxApp.Text = Me._TF3DRSKApp
    ka.TextBoxText.Text = Me._TF3DRSKText
    If ka.ShowDialog = Windows.Forms.DialogResult.OK Then
      Me._TF3DRSKApp = ka.TextBoxApp.Text
      Me._TF3DRSKText = ka.TextBoxText.Text
    End If
  End Sub

  Private Sub ButtonTodayLeftSoftKey_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonTodayLeftSoftKey.Click
    Dim ka As New KeyAssigner
    ka.LabelAssign.Text = "Assign Today left soft Key"
    ka.TextBoxApp.Text = Me._todayLSKApp
    ka.TextBoxText.Text = Me._todayLSKText
    If ka.ShowDialog = Windows.Forms.DialogResult.OK Then
      Me._todayLSKApp = ka.TextBoxApp.Text
      Me._todayLSKText = ka.TextBoxText.Text
    End If
  End Sub

  Private Sub ButtonTodayRightSoftKey_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonTodayRightSoftKey.Click
    Dim ka As New KeyAssigner
    ka.LabelAssign.Text = "Assign Today right soft Key"
    ka.TextBoxApp.Text = Me._todayRSKApp
    ka.TextBoxText.Text = Me._todayRSKText
    If ka.ShowDialog = Windows.Forms.DialogResult.OK Then
      Me._todayRSKApp = ka.TextBoxApp.Text
      Me._todayRSKText = ka.TextBoxText.Text
    End If
  End Sub


  Private Sub LinkLabelRemoveFromNotQueue_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LinkLabelRemoveFromNotQueue.Click
    Dim count As Integer = 0
    If MessageBox.Show("The ManilaActivateToday.exe application runs at midnight every night and can interfere with TouchFlo Detacher.  It is recommended that you remove it from the notification queue.  Would you like to proceed?", "Proceed?", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) = Windows.Forms.DialogResult.Yes Then
      Cursor.Current = Cursors.WaitCursor

      For Each hnd As Integer In OpenNETCF.WindowsCE.Notification.Notify.GetUserNotificationHandles
        Dim un As OpenNETCF.WindowsCE.Notification.UserNotificationInfoHeader = OpenNETCF.WindowsCE.Notification.Notify.GetUserNotification(hnd)
        If un IsNot Nothing AndAlso un.UserNotificationTrigger IsNot Nothing AndAlso un.UserNotificationTrigger.Application IsNot Nothing Then
          If un.UserNotificationTrigger.Application.ToLower = "\windows\manilaactivatetoday.exe" Then
            OpenNETCF.WindowsCE.Notification.Notify.ClearUserNotification(hnd)
            count += 1
          End If
        End If
      Next

      Cursor.Current = Cursors.Default

      If count = 1 Then
        MessageBox.Show("Removed one occurance of ManilaActivateToday.exe")
      ElseIf count > 1 Then
        MessageBox.Show("Removed " & count.ToString & " occurances of ManilaActivateToday.exe")
      Else
        MessageBox.Show("ManilaActivateToday.exe was not found in the notification queue")
      End If
    End If
  End Sub


  ' Loads configuration values from the registry
  Private Sub LoadRegistry()
    Dim p As New Process
    p.StartInfo.UseShellExecute = True
    p.StartInfo.FileName = "http://www.amazon.com/"
    p.Start()

    ' Settings
    Me.CheckBoxLaunchOnStartup.Checked = TFDRegistrySettings.LaunchTouchFloOnStartup
    Me.NumericUpDownLaunchDelay.Value = CDec(TFDRegistrySettings.LaunchTouchFloOnStartupDelayInSeconds)
    Me.CheckBoxDialerWorkaround.Checked = TFDRegistrySettings.LaunchTouchFloDialerWorkaround
    Me.TextBoxMainWindowClass.Text = TFDRegistrySettings.TouchFloMainWindowClass
    Me.TextBoxSoftKeyWindowClass.Text = TFDRegistrySettings.TouchFloSoftKeyWindowClass
    Me.TextBoxFullPath.Text = TFDRegistrySettings.TouchFloFullPath
    Me.TextBoxEXEOnly.Text = TFDRegistrySettings.TouchFloEXEOnly
    Me.TextBoxSKBaseKey.Text = TFDRegistrySettings.ManilaSoftKeyBaseKey
    Me.TextBoxLSKCmdValue.Text = TFDRegistrySettings.TF3DLeftSoftKeyCmdValue
    Me.TextBoxLSKTitleValue.Text = TFDRegistrySettings.TF3DLeftSoftKeyNameValue
    Me.TextBoxRSKCmdValue.Text = TFDRegistrySettings.TF3DRightSoftKeyCmdValue
    Me.TextBoxRSKTitleValue.Text = TFDRegistrySettings.TF3DRightSoftKeyNameValue
    Me._touchFloMode = TFDRegistrySettings.TouchFloMode
    Me.CheckBoxLaunchApp.Checked = TFDRegistrySettings.LaunchAppAfterTouchFlo
    Me.TextBoxOtherApp.Text = TFDRegistrySettings.LaunchAppAfterTouchFloPath
    Me.NumericUpDownLaunchAppSec.Value = CDec(TFDRegistrySettings.LaunchAppAfterTouchFloDelayInSeconds)

    ' Soft keys
    Me._TF3DLSKApp = TFDRegistrySettings.TF3DLeftSoftKeyApp
    Me._TF3DLSKText = TFDRegistrySettings.TF3DLeftSoftKeyText
    Me._TF3DRSKApp = TFDRegistrySettings.TF3DRightSoftKeyApp
    Me._TF3DRSKText = TFDRegistrySettings.TF3DRightSoftKeyText
    Me._todayLSKApp = TFDRegistrySettings.TodayLeftSoftKeyApp
    Me._todayLSKText = TFDRegistrySettings.TodayLeftSoftKeyText
    Me._todayRSKApp = TFDRegistrySettings.TodayRightSoftKeyApp
    Me._todayRSKText = TFDRegistrySettings.TodayRightSoftKeyText

    ' Set the radio button
    Me.SetTouchFloMode()

    ' Set the inactive state
    CheckBoxLaunchApp_CheckStateChanged(Me, EventArgs.Empty)
  End Sub

  ' Saves configuration values back to the registry
  Private Sub SaveRegistry()
    ' Settings
    TFDRegistrySettings.LaunchTouchFloOnStartup = Me.CheckBoxLaunchOnStartup.Checked
    TFDRegistrySettings.LaunchTouchFloOnStartupDelayInSeconds = CInt(Me.NumericUpDownLaunchDelay.Value)
    TFDRegistrySettings.LaunchTouchFloDialerWorkaround = Me.CheckBoxDialerWorkaround.Checked
    TFDRegistrySettings.TouchFloMainWindowClass = Me.TextBoxMainWindowClass.Text
    TFDRegistrySettings.TouchFloSoftKeyWindowClass = Me.TextBoxSoftKeyWindowClass.Text
    TFDRegistrySettings.TouchFloFullPath = Me.TextBoxFullPath.Text
    TFDRegistrySettings.TouchFloEXEOnly = Me.TextBoxEXEOnly.Text
    TFDRegistrySettings.ManilaSoftKeyBaseKey = Me.TextBoxSKBaseKey.Text
    TFDRegistrySettings.TF3DLeftSoftKeyCmdValue = Me.TextBoxLSKCmdValue.Text
    TFDRegistrySettings.TF3DLeftSoftKeyNameValue = Me.TextBoxLSKTitleValue.Text
    TFDRegistrySettings.TF3DRightSoftKeyCmdValue = Me.TextBoxRSKCmdValue.Text
    TFDRegistrySettings.TF3DRightSoftKeyNameValue = Me.TextBoxRSKTitleValue.Text
    TFDRegistrySettings.TouchFloMode = Me._touchFloMode
    TFDRegistrySettings.LaunchAppAfterTouchFlo = Me.CheckBoxLaunchApp.Checked
    TFDRegistrySettings.LaunchAppAfterTouchFloPath = Me.TextBoxOtherApp.Text
    TFDRegistrySettings.LaunchAppAfterTouchFloDelayInSeconds = CInt(Me.NumericUpDownLaunchAppSec.Value)

    ' Softkeys
    If ButtonTFLeftSoftKey.Enabled Then TFDRegistrySettings.TF3DLeftSoftKeyApp = Me._TF3DLSKApp
    If ButtonTFLeftSoftKey.Enabled Then TFDRegistrySettings.TF3DLeftSoftKeyText = Me._TF3DLSKText
    If ButtonTFRightSoftKey.Enabled Then TFDRegistrySettings.TF3DRightSoftKeyApp = Me._TF3DRSKApp
    If ButtonTFRightSoftKey.Enabled Then TFDRegistrySettings.TF3DRightSoftKeyText = Me._TF3DRSKText
    TFDRegistrySettings.TodayLeftSoftKeyApp = Me._todayLSKApp
    TFDRegistrySettings.TodayLeftSoftKeyText = Me._todayLSKText
    TFDRegistrySettings.TodayRightSoftKeyApp = Me._todayRSKApp
    TFDRegistrySettings.TodayRightSoftKeyText = Me._todayRSKText
  End Sub

  Private Sub CheckBoxLaunchOnStartup_CheckStateChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBoxLaunchOnStartup.CheckStateChanged, CheckBoxDialerWorkaround.CheckStateChanged
    Me.LabelLaunch.Enabled = CheckBoxLaunchOnStartup.Checked
    Me.NumericUpDownLaunchDelay.Enabled = CheckBoxLaunchOnStartup.Checked
  End Sub

  Private Sub SetTouchFloMode()
    Me._warnOnRadioChange = False
    Select Case Me._touchFloMode
      Case 0
        Me.ComboBoxMode.SelectedIndex = 0
        Me._lastIndex = 0
        Me.PanelAdvancedInner.Enabled = False
      Case 1
        Me.ComboBoxMode.SelectedIndex = 2
        Me._lastIndex = 2
        Me.PanelAdvancedInner.Enabled = False
      Case 3
        Me.ComboBoxMode.SelectedIndex = 1
        Me._lastIndex = 1
        Me.PanelAdvancedInner.Enabled = False
      Case Else
        Me.ComboBoxMode.SelectedIndex = 3
        Me._lastIndex = 3
        Me.PanelAdvancedInner.Enabled = True
    End Select
    Me._warnOnRadioChange = True
  End Sub

  Private Sub ComboBoxMode_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBoxMode.SelectedIndexChanged
    Static recurse As Boolean = True
    If recurse AndAlso Me._warnOnRadioChange Then
      recurse = False

      If ComboBoxMode.SelectedIndex < 3 Then
        If MessageBox.Show("Are you sure you want to set ADVANCED values with the " & ComboBoxMode.SelectedItem.ToString & " defaults?  If you select ""yes"" then you will not be able to modify TouchFlo softkeys until you save and restart TFDConfig.", "Are you sure?", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) = Windows.Forms.DialogResult.Yes Then
          Me._lastIndex = Me.ComboBoxMode.SelectedIndex

          Select Case Me.ComboBoxMode.SelectedIndex
            Case 0
              Me.TextBoxMainWindowClass.Text = "Manila"
              Me.TextBoxSoftKeyWindowClass.Text = "menu_worker"
              Me.TextBoxEXEOnly.Text = "manila.exe"
              Me.TextBoxFullPath.Text = "\windows\manila.exe"
              Me.TextBoxSKBaseKey.Text = "Software\HTC\Manila"
              Me.TextBoxLSKCmdValue.Text = "HomeLSKPath"
              Me.TextBoxLSKTitleValue.Text = "HomeLSKText"
              Me.TextBoxRSKCmdValue.Text = "HomeRSKPath"
              Me.TextBoxRSKTitleValue.Text = "HomeRSKText"
              Me.ButtonTFLeftSoftKey.Enabled = False
              Me.ButtonTFRightSoftKey.Enabled = False
              Me.PanelAdvancedInner.Enabled = False
              Me._touchFloMode = 0
            Case 1
              Me.TextBoxMainWindowClass.Text = "Manila"
              Me.TextBoxSoftKeyWindowClass.Text = "menu_worker"
              Me.TextBoxEXEOnly.Text = "manil2.exe"
              Me.TextBoxFullPath.Text = "\windows\manila.exe"
              Me.TextBoxSKBaseKey.Text = "Software\HTC\Manila"
              Me.TextBoxLSKCmdValue.Text = "HomeLSKPath"
              Me.TextBoxLSKTitleValue.Text = "HomeLSKText"
              Me.TextBoxRSKCmdValue.Text = "HomeRSKPath"
              Me.TextBoxRSKTitleValue.Text = "HomeRSKText"
              Me.ButtonTFLeftSoftKey.Enabled = False
              Me.ButtonTFRightSoftKey.Enabled = False
              Me.PanelAdvancedInner.Enabled = False
              Me._touchFloMode = 3
            Case 2
              Me.TextBoxMainWindowClass.Text = "HTCManila2D"
              Me.TextBoxSoftKeyWindowClass.Text = "menu_worker"
              Me.TextBoxEXEOnly.Text = "manila2d.exe"
              Me.TextBoxFullPath.Text = "\windows\manila2d.exe"
              Me.TextBoxSKBaseKey.Text = "Software\HTC\Manila2D\Home"
              Me.TextBoxLSKCmdValue.Text = "SK1Cmd"
              Me.TextBoxLSKTitleValue.Text = "SK1"
              Me.TextBoxRSKCmdValue.Text = "SK2Cmd"
              Me.TextBoxRSKTitleValue.Text = "SK2"
              Me.ButtonTFLeftSoftKey.Enabled = False
              Me.ButtonTFRightSoftKey.Enabled = False
              Me.PanelAdvancedInner.Enabled = False
              Me._touchFloMode = 1
          End Select
        Else
          Me.ComboBoxMode.SelectedIndex = Me._lastIndex
        End If
      Else ' Custom mode
        Me.PanelAdvancedInner.Enabled = True
        Me._touchFloMode = 2
      End If
    End If
    recurse = True
  End Sub

  Private Sub TextBoxSKBaseKey_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBoxSKBaseKey.TextChanged
    If TextBoxSKBaseKey.Enabled Then
      Me.ButtonTFLeftSoftKey.Enabled = False
      Me.ButtonTFRightSoftKey.Enabled = False
    End If
  End Sub

  Private Sub TextBoxSKBaseKey_GotFocus(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBoxSKBaseKey.GotFocus
    Static showWarn As Boolean = True
    If TextBoxSKBaseKey.Enabled AndAlso showWarn Then
      showWarn = False
      MessageBox.Show("Note: if you change the registry base key, you will not be able to modify TouchFlo softkeys until you save and restart TFDConfig.", "Notice", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1)
    End If
  End Sub

  Private Sub ButtonHideShowExplain_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonHideShowExplain.Click
    MessageBox.Show("Sometimes after a soft reset, the dialer will not launch until TouchFlo has been hidden and shown at least once.  Checking this box will have TouchFlo Detacher do it automatically for you.", "Help", MessageBoxButtons.OK, MessageBoxIcon.None, MessageBoxDefaultButton.Button1)
  End Sub

  Private Sub CheckBoxLaunchApp_CheckStateChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBoxLaunchApp.CheckStateChanged
    Me.LabelL1.Enabled = CheckBoxLaunchApp.Checked
    Me.LabelL2.Enabled = CheckBoxLaunchApp.Checked
    Me.TextBoxOtherApp.Enabled = CheckBoxLaunchApp.Checked
    Me.NumericUpDownLaunchAppSec.Enabled = CheckBoxLaunchApp.Checked
  End Sub
End Class
